<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-02-24
 * Time: 14:18
 */

namespace app\model;

class Job extends BaseModel{

    function lists($where = [], $where1= '', $page=1, $page_size = 10){

        $where['status'] = 2;
        $lat = $where['lat'];
        $lng = $where['lng'];

        unset($where['lat'], $where['lng']);

        $result = db('job')
            ->where($where)
            ->where($where1)
            ->field("id, name, pay, publish_time, address, (st_distance (point (lng, lat),point($lng,$lat) ) * 111195) AS distance, company_id, area_tag")
            ->page($page, $page_size)
            ->order('distance asc')
            ->select();

//        exit(db('job')->getLastSql());
        $area = self::area_tag();
        $company = self::company();

        foreach ($result as &$v){

            $tag = db('job_tag')->where(['job_id'=> $v['id']])->column('w_id');
            $v['tag'] = $tag;
            $v['publish_time'] = date('Y-m-d H:i', strtotime($v['publish_time']));
            $v['area'] = $area[$v['area_tag']]?:'';
            $v['company'] = $company[$v['company_id']]?:'';

            if(intval($v['distance']) < 1000)
            {
                $v['distance'] = intval($v['distance'])."米";
            }else{
                $v['distance'] = round(intval($v['distance'])/1000, 2).'公里';
            }

        }

        return $result;
    }

    static function area_tag(){
        $data = db('area_tag')->field('id, name')->select();
        return array_column($data, "name", "id");
    }

    static function company(){
        $data = db('company')->field('id, name')->select();
        return array_column($data, "name", "id");
    }


}